package com.ptswitch.material.service.impl;

import java.util.List;

import org.hxzon.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ptswitch.material.dao.BomMapper;
import com.ptswitch.material.dao.BomMaterialMapper;
import com.ptswitch.material.dao.OrigFileMapper;
import com.ptswitch.material.model.material.Bom;
import com.ptswitch.material.model.material.BomMaterial;
import com.ptswitch.material.service.BomService;

@Service
public class BomServiceImpl implements BomService {

    @Autowired
    private BomMapper bomMapper;
    @Autowired
    private BomMaterialMapper bomMaterialMapper;
    @Autowired
    private OrigFileMapper origFileMapper;

    @Override
    public void deleteAll() {
        for (Bom bom : bomMapper.findAll(-1, -1)) {
            delete(bom);
        }
    }

    @Override
    public Bom findById(long id, int init) {
        Bom bom = bomMapper.findById(id);
        return bom;
    }

    @Override
    public Bom findById(long id) {
        return bomMapper.findById(id);
    }

    @Override
    public Bom findByCodeAndVersion(String code, String version) {
        code = StringUtil.escapeSql(code);
        version = StringUtil.escapeSql(version);
        return bomMapper.findByCodeAndVersion(code, version);
    }

    @Override
    public void update(Bom oldModel, Bom newModel) {
        //FIXME bom materials and upload delete cascade
        // dao.deleteAll(oldModel.getBomMaterials());
        bomMapper.delete(oldModel);
        // dao.delete(oldModel.getUpload());
        // add new
        origFileMapper.save(newModel.getOrigFile());
        bomMapper.add(newModel);
        // newModel.getUpload().setOwnerId(newModel.getId());
        // newModel.getUpload().setOwnerType("bomSource");
        addBomMaterials(newModel);
    }

    @Override
    public void save(Bom bom) {
        bomMapper.add(bom);
    }

    @Override
    public void saveEx(Bom model) {
        origFileMapper.save(model.getOrigFile());
        bomMapper.add(model);
//		model.getUpload().setCreateTime(DateUtil.formatToNano(new Date()));
        // model.getUpload().setOwnerType("bomSource");
        // model.getUpload().setOwnerId(model.getId());
        addBomMaterials(model);
    }

    private void addBomMaterials(Bom bom) {
        for (BomMaterial bm : bom.getBomMaterials()) {
            bomMaterialMapper.add(bm);
        }
    }

    public String valid(Bom model) {
        String error = "";
        if (model.getCode() == null || model.getCode().isEmpty()) {
            error += "编码不能为空";
        }
        if (model.getVersion() == null || model.getVersion().isEmpty()) {
            error += "版本号不能为空";
        }
        return error;
    }

    @Override
    public void delete(Bom bom) {
        for (BomMaterial bm : bom.getBomMaterials()) {
            bomMaterialMapper.delete(bm);
        }
        bomMapper.delete(bom);
    }

    @Override
    public List<Bom> findAll(long first, long size) {
        return bomMapper.findAll(first, size);
    }

    @Override
    public long countAll() {
        return bomMapper.countAll();
    }

    @Override
    public List<Bom> findLike(String like, long first, long size) {
        return bomMapper.findLike(like, first, size);
    }

    @Override
    public long countLike(String like) {
        return bomMapper.countLike(like);
    }

}
